'''
* This is the projet for Brtc LlmOps Platform
* @Author Leon-liao <liaosiliang@alltman.com>
* @Description //TODO 
* @File: 8_study_llm_rollback.py
* @Time: 2025/7/27
* @All Rights Reserve By Brtc
'''
import dotenv
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI

"""7、Runnable 组件重试 / 回退机制降低程序错误率 b、Runnable回退机制 """
dotenv.load_dotenv()

# 1、构建prompt与LLM 并将model切换成其他model
prompt = ChatPromptTemplate.from_template("{query}")
llm = ChatOpenAI(model = "gpt-4k").with_fallbacks([ChatOpenAI(model = "gpt-3.5o"), ChatOpenAI(model = "gpt-4o")])

chain = prompt|llm|StrOutputParser()

content = chain.invoke({"query":"请你是Gpt几？"})
print(content)